var container;

var camera, scene, renderer, mesh;

var mouseX = 0,
    mouseY = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;


init();
animate();


function init() {

    container = document.createElement('div');
    document.body.appendChild(container);

    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 4000);
    // camera.position.z = 60;
    camera.position.set(0, 50, 50);
    camera.lookAt({
        x: 0,
        y: 0,
        z: 0
    });

    // scene

    scene = new THREE.Scene();

    var ambient = new THREE.AmbientLight(0x101030);
    scene.add(ambient);

    var directionalLightOne = new THREE.DirectionalLight(0xffffff);
    var directionalLightTwo = new THREE.DirectionalLight(0xffffff);
    var directionalLightThree = new THREE.DirectionalLight(0x656565);


    directionalLightOne.position.set(1, 1, 1);
    directionalLightTwo.position.set(-1, -1, -1);


    scene.add(directionalLightOne);
    scene.add(directionalLightTwo);
    scene.add(directionalLightThree);



    // texture

    var manager = new THREE.LoadingManager();
    manager.onProgress = function(item, loaded, total) {

        console.log(item, loaded, total);

    };

    var texture = new THREE.Texture();

    var onProgress = function(xhr) {
        if (xhr.lengthComputable) {
            var percentComplete = xhr.loaded / xhr.total * 100;
            console.log(Math.round(percentComplete, 2) + '% downloaded');
            var LoadingNumber = Math.round(percentComplete, 2);

            document.getElementById('LoadingNumber').innerHTML = LoadingNumber + '%';

            if (LoadingNumber == 100) {
                var t=setTimeout("document.getElementById('LoadingGIF').style.display = 'none';",900);
            };
        }
    };

    var onError = function(xhr) {};


    var loader = new THREE.ImageLoader(manager);
    loader.load('textures/model-test5.jpg', function(image) {

        texture.image = image;
        texture.needsUpdate = true;

    });


    // model

    var loader = new THREE.OBJLoader(manager);
    loader.load('models/model-test5.obj', function(object) {

        object.traverse(function(child) {

            if (child instanceof THREE.Mesh) {

                child.material.map = texture;

            }

        });

        mesh = object;
        mesh.position.x = 0;
        mesh.position.y = 0;
        mesh.position.z = 0;

        scene.add(mesh);


    }, onProgress, onError);

    //

    renderer = new THREE.WebGLRenderer();
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setClearColor(0x333333, 0.85);
    container.appendChild(renderer.domElement);

    /*document.addEventListener( 'mousemove', onDocumentMouseMove, false );*/

    //
    var controls = new THREE.OrbitControls(camera, renderer.domElement);
    controls.update();

    window.addEventListener('resize', onWindowResize, false);

}

function onWindowResize() {

    windowHalfX = window.innerWidth / 2;
    windowHalfY = window.innerHeight / 2;

    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize(window.innerWidth, window.innerHeight);

}

/*function onDocumentMouseMove( event ) {

    mouseX = ( event.clientX - windowHalfX ) / 2;
    mouseY = ( event.clientY - windowHalfY ) / 2;

}*/

//

function animate() {

    requestAnimationFrame(animate);
    render();

}

function render() {

    /*  camera.position.x += ( mouseX - camera.position.x ) * .05;
        camera.position.y += ( - mouseY - camera.position.y ) * .05;*/

    camera.lookAt(scene.position);

    renderer.render(scene, camera);

}



